package com.itheima.task;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.extension.api.R;
import com.itheima.domain.mongo.Movement;
import com.itheima.domain.mongo.RecommendMovement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Set;

/**
 *  目的：读取redis里的推荐数据    将数据放入mongo中
 *  1.现将mongo中数据都删除（先删除给该用户推荐的数据）
 *  2.不喜欢位置上    删除mongo数据
 */
@Component
public class RecommendMovementTask {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Scheduled(cron = "0 0 0/1 * * ?") // 0 0/1 * * * ?
//    @Scheduled(cron = "0 0/1 * * * ? ")
    public void recommend() {

        //1.先读取redis缓存数据    keys *  能够获取所有的key
        Set<String> keys = stringRedisTemplate.keys("QUANZI_PUBLISH_RECOMMEND_*");
        if (CollectionUtil.isNotEmpty(keys)) {
            for (String key : keys) {
                //替换之前的数据
                String redisValue = stringRedisTemplate.opsForValue().get(key);
                //2.拿到每一个人推送的id（用户） QUANZI_PUBLISH_RECOMMEND_1      QUANZI_PUBLISH_RECOMMEND_2
                key = key.replaceAll("QUANZI_PUBLISH_RECOMMEND_", "");
                //2.先删除原来的mongo中的数据
                Query query = new Query(
                        Criteria.where("userId").is(Long.valueOf(key))//注意数据类型的转换
                );
                mongoTemplate.remove(query, RecommendMovement.class);
                //3.拿到redis给每一个人推送的数据
                //3.1将新数据存储到mongo中
                String[] pidArray = redisValue.split(",");//动态id的数组（自己生成的pid）
                //3.2拿到每一个pid
                for (String pidStr : pidArray) {
                    //3.3将数据存储到mongo中
                    RecommendMovement recommendMovement = new RecommendMovement();
                    recommendMovement.setCreated(System.currentTimeMillis());//时间戳
                    recommendMovement.setUserId(Long.valueOf(key));//给谁推送
                    recommendMovement.setPid(Long.valueOf(pidStr));//推送的动态id    大数据产生的动态id
                    //根据pid查询到  动态详情
                    Query movementQuery = new Query(
                            Criteria.where("pid").is(Long.valueOf(pidStr))
                    );

                    Movement movement = mongoTemplate.findOne(movementQuery, Movement.class);
                    recommendMovement.setPublishId(movement.getId());//动态id mongo中存储的   objectId    动态id
                    //推荐的分数（大数据应该会产生一个分数值（缘分值） 但是此处没有推送，只能自己构造）
                    recommendMovement.setScore(RandomUtil.randomDouble(70,90));
                    mongoTemplate.save(recommendMovement);
                }
                //4.删除redis缓存
                stringRedisTemplate.delete("QUANZI_PUBLISH_RECOMMEND_" + key);
            }

        }
        System.out.println("结束推荐！！！");

    }
}
